﻿<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->

<UserControl x:Class="Microsoft.Windows.Controls.Samples.DataGridAutoCompleteBoxEdit"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:samples="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples"
    xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls">
    <StackPanel>
        <StackPanel.Resources>
            <samples:SampleEmployeeCollection x:Key="SampleEmployees" />
        </StackPanel.Resources>
        
        <ContentControl Content="AutoCompleteBox inside a DataGrid editing cell template" Style="{StaticResource Header}" />
        
        <StackPanel>
            <TextBlock UseLayoutRounding="False">
                This example sets the DataGridTemplateColumn.CellEditingTemplate 
                to an AutoCompleteBox control that is bound to a set of known 
                names.
                <LineBreak /><LineBreak />
                
                Double-click on a column with a name to display the 
                AutoCompleteBox.
                <LineBreak />
            </TextBlock>
            
            <data:DataGrid x:Name="MyDataGrid" AutoGenerateColumns="False">
                <data:DataGrid.Columns>
                    <data:DataGridTextColumn Binding="{Binding RandomNumber}" Header="Random Number" />
                    <data:DataGridTemplateColumn Header="Name">
                        <data:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                               <TextBlock Text="{Binding Name}" Margin="4" />
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellTemplate>
                        <data:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <controls:AutoCompleteBox 
                                    HorizontalAlignment="Left"
                                    Width="180"
                                    IsTabStop="True"
                                    ItemsSource="{StaticResource SampleEmployees}"
                                    Text="{Binding Name, Mode=TwoWay}"
                                    />
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellEditingTemplate>
                    </data:DataGridTemplateColumn>
                    <data:DataGridCheckBoxColumn Binding="{Binding RandomTrueFalse}" Header="Random Bool" />
                </data:DataGrid.Columns>
            </data:DataGrid>
        </StackPanel>

        <src:SourceViewer xmlns:src="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <src:SourceFile Path="DataGridAutoCompleteBoxEdit.xaml">
    <src:SourceFile.Source>
      <sys:String>&lt;!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
--&gt;

&lt;UserControl x:Class="Microsoft.Windows.Controls.Samples.DataGridAutoCompleteBoxEdit"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:samples="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples"
    xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"&gt;
    &lt;StackPanel&gt;
        &lt;StackPanel.Resources&gt;
            &lt;samples:SampleEmployeeCollection x:Key="SampleEmployees" /&gt;
        &lt;/StackPanel.Resources&gt;
        
        &lt;ContentControl Content="AutoCompleteBox inside a DataGrid editing cell template" Style="{StaticResource Header}" /&gt;
        
        &lt;StackPanel&gt;
            &lt;TextBlock UseLayoutRounding="False"&gt;
                This example sets the DataGridTemplateColumn.CellEditingTemplate 
                to an AutoCompleteBox control that is bound to a set of known 
                names.
                &lt;LineBreak /&gt;&lt;LineBreak /&gt;
                
                Double-click on a column with a name to display the 
                AutoCompleteBox.
                &lt;LineBreak /&gt;
            &lt;/TextBlock&gt;
            
            &lt;data:DataGrid x:Name="MyDataGrid" AutoGenerateColumns="False"&gt;
                &lt;data:DataGrid.Columns&gt;
                    &lt;data:DataGridTextColumn Binding="{Binding RandomNumber}" Header="Random Number" /&gt;
                    &lt;data:DataGridTemplateColumn Header="Name"&gt;
                        &lt;data:DataGridTemplateColumn.CellTemplate&gt;
                            &lt;DataTemplate&gt;
                               &lt;TextBlock Text="{Binding Name}" Margin="4" /&gt;
                            &lt;/DataTemplate&gt;
                        &lt;/data:DataGridTemplateColumn.CellTemplate&gt;
                        &lt;data:DataGridTemplateColumn.CellEditingTemplate&gt;
                            &lt;DataTemplate&gt;
                                &lt;controls:AutoCompleteBox 
                                    HorizontalAlignment="Left"
                                    Width="180"
                                    IsTabStop="True"
                                    ItemsSource="{StaticResource SampleEmployees}"
                                    Text="{Binding Name, Mode=TwoWay}"
                                    /&gt;
                            &lt;/DataTemplate&gt;
                        &lt;/data:DataGridTemplateColumn.CellEditingTemplate&gt;
                    &lt;/data:DataGridTemplateColumn&gt;
                    &lt;data:DataGridCheckBoxColumn Binding="{Binding RandomTrueFalse}" Header="Random Bool" /&gt;
                &lt;/data:DataGrid.Columns&gt;
            &lt;/data:DataGrid&gt;
        &lt;/StackPanel&gt;
    &lt;/StackPanel&gt;
&lt;/UserControl&gt;
</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="DataGridAutoCompleteBoxEdit.xaml.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace Microsoft.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// The DataGridAutoCompleteBoxEdit class selects a small set of data to 
    /// display in the DataGrid. The XAML file contains the custom editing 
    /// template for AutoCompleteBox.
    /// &lt;/summary&gt;
    [Sample("DataGrid Editing", DifficultyLevel.Advanced)]
    [Category("AutoCompleteBox")]
    public partial class DataGridAutoCompleteBoxEdit : UserControl
    {
        /// &lt;summary&gt;
        /// Initializes a new instance of the DataGridAutoCompleteBoxEdit type.
        /// &lt;/summary&gt;
        public DataGridAutoCompleteBoxEdit()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        /// &lt;summary&gt;
        /// Handle the Loaded event of the page. This creates a small, random 
        /// set of data to display in the grid.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event arguments.&lt;/param&gt;
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            List&lt;RandomEmployeeDetails&gt; data = new List&lt;RandomEmployeeDetails&gt;();
            Random random = new Random();

            // Select up to 8 random employees
            foreach (Employee employee in 
                new SampleEmployeeCollection()
                    .Where(item =&gt; random.Next(2) == 0)
                    .Distinct()
                    .Take(8))
            {
                data.Add(new RandomEmployeeDetails(employee));
            }
            MyDataGrid.ItemsSource = data;
        }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="RandomEmployeeDetails.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Microsoft.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// RandomEmployeeDetails is a sample type that contains mostly random data 
    /// for use in DataGrid sample scenarios.
    /// &lt;/summary&gt;
    public class RandomEmployeeDetails
    {
        /// &lt;summary&gt;
        /// A random number generator.
        /// &lt;/summary&gt;
        private static Random RandomGenerator = new Random();

        /// &lt;summary&gt;
        /// Initializes a new instance of the RandomEmployeeDetails type. A 
        /// random number and bool value will be generated in the constructor.
        /// &lt;/summary&gt;
        public RandomEmployeeDetails()
        {
            RandomNumber = RandomGenerator.Next();
            RandomTrueFalse = RandomGenerator.Next(0, 2) == 1;
        }

        /// &lt;summary&gt;
        /// Initializes a new instance of the RandomEmployeeDetails type.
        /// &lt;/summary&gt;
        /// &lt;param name="employee"&gt;An Employee object to read the Name from.&lt;/param&gt;
        public RandomEmployeeDetails(Employee employee) : this()
        {
            Name = employee.DisplayName;
        }

        /// &lt;summary&gt;
        /// Gets or sets a name.
        /// &lt;/summary&gt;
        public string Name { get; set; }

        /// &lt;summary&gt;
        /// Gets or sets a random number.
        /// &lt;/summary&gt;
        public int RandomNumber { get; set; }

        /// &lt;summary&gt;
        /// Gets or sets a value indicating whether the value is true or false.
        /// &lt;/summary&gt;
        public bool RandomTrueFalse { get; set; }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="SampleEmployeeCollection.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections;
using Microsoft.Windows.Controls;

namespace Microsoft.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// A collection type that makes it easy to place sample employee data into 
    /// XAML.
    /// &lt;/summary&gt;
    public class SampleEmployeeCollection : ObjectCollection
    {
        /// &lt;summary&gt;
        /// Initializes a new instance of the SampleEmployeeCollection class.
        /// &lt;/summary&gt;
        public SampleEmployeeCollection()
            : base(Employee.AllExecutives)
        {
        }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
</src:SourceViewer>
    </StackPanel>
</UserControl>
